﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Tilver.Api.Weibo;
using Weibo.Webservice.Service;

namespace Weibo.Webservice
{
    /// <summary>
    /// SinaService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://service.tilver.cn/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class SinaService : System.Web.Services.WebService
    {
        public WeiboSoapHeader weiboHeader = new WeiboSoapHeader();

        [WebMethod(Description = "根据code获取AccessToken")]
        [SoapHeader("weiboHeader")]
        public WebServiceResult<string> GetAccessTokenByAuthorizationCode(string code)
        {
            try
            {
                var sinaClient = new SinaClient(weiboHeader.AppKey, weiboHeader.AppSecret, weiboHeader.CallbackUrl);
                var token = sinaClient.GetAccessTokenByAuthorizationCode(code);
                if (token == null) return new WebServiceResult<string>() { State = 1, Message = "Get AccessToken Fail!", Value = string.Empty };
                return new WebServiceResult<string>() { State = 0, Message = "Success!", Value = token };
            }
            catch (Exception ex)
            {
                return new WebServiceResult<string>() { State = 2, Message = ex.Message, Value = string.Empty };
            }

        }

        [WebMethod(Description = "根据用户名和密码获取AccessToken")]
        [SoapHeader("weiboHeader")]
        public WebServiceResult<string> GetAccessTokenByPassword(string userId, string password)
        {
            try
            {
                var sinaClient = new SinaClient(weiboHeader.AppKey, weiboHeader.AppSecret, weiboHeader.CallbackUrl);
                var token = sinaClient.GetAccessTokenByPassword(userId, password);
                if (token == null) return new WebServiceResult<string>() { State = 1, Message = "Get AccessToken Fail!", Value = string.Empty };
                return new WebServiceResult<string>() { State = 0, Message = "Success!", Value = token };
            }
            catch (Exception ex)
            {
                return new WebServiceResult<string>() { State = 2, Message = ex.Message, Value = string.Empty };
            }

        }

        [WebMethod(Description = "发布一条新微博")]
        [SoapHeader("weiboHeader")]
        public WebServiceResult<bool> StatusesUpdate(string content)
        {
            try
            {
                var sinaClient = new SinaClient(weiboHeader.AppKey, weiboHeader.AppSecret, weiboHeader.AccessToken, weiboHeader.RefreshToken);
                var result = sinaClient.SendCommand("statuses/update",
                    RequestMethod.Post,
                    new RequestParameter("status", string.Format("{0}", content)));
                return new WebServiceResult<bool>() { State = 0, Message = "Success!", Value = true };
            }
            catch (Exception ex)
            {
                return new WebServiceResult<bool>() { State = 2, Message = ex.Message, Value = false };
            }

        }

        [WebMethod(Description = "上传图片并发布一条新微博")]
        [SoapHeader("weiboHeader")]
        public WebServiceResult<bool> StatusesUpload(string content, byte[] pic)
        {
            try
            {
                var sinaClient = new SinaClient(weiboHeader.AppKey, weiboHeader.AppSecret, weiboHeader.AccessToken, weiboHeader.RefreshToken);
                var result = sinaClient.SendCommand("statuses/upload",
                    RequestMethod.Post,
                    new RequestParameter("status", string.Format("{0}", content)),
                    new RequestParameter("pic", pic));
                return new WebServiceResult<bool>() { State = 0, Message = "Success!", Value = true };
            }
            catch (Exception ex)
            {
                return new WebServiceResult<bool>() { State = 2, Message = ex.Message, Value = false };
            }

        }
    }
}
